import { Component } from '@angular/core';
import {
  AlertController, App, IonicPage, LoadingController, NavController, NavParams, Platform,
  ToastController
} from 'ionic-angular';
import { ApiProvider } from "../../providers/api/api";
import {DatePipe} from "@angular/common";
import { TeachingPage } from '../teaching/teaching';

/**
 * Generated class for the StudentAddPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-student-add',
  templateUrl: 'student-add.html',
})
export class StudentAddPage {

  constructor(private alertCtrl: AlertController,public navCtrl: NavController, public navParams: NavParams,
    public loadingCtrl: LoadingController,
    public http: ApiProvider,private dataPipe:DatePipe,
  ) {
  }
  classcourseId;
  classcourseNum;
  unitId;
  page = 1;
  pageNum;
  StudentList;
  teacherId;
  slides:Array<any> = [];
  total; //数据数
  ID;
  isr;


  ionViewDidLoad() {
    //classcourseId:this.classCourseId,classcourseNum:this.classcourse_num,unitId:this.unitId
    this.classcourseId = this.navParams.get("classcourseId");
    this.classcourseNum=this.navParams.get("classcourseNum");
    this.unitId = this.navParams.get("unitId");
    this.teacherId=localStorage.getItem('teacherId');
  }
  ionViewDidEnter() {
    // console.log("ionViewDidEnter run");
    this.LoadList(1);
  }

  /**获取列表 */
  LoadList(e){
    this.http.post("/teacherStudentController/getStudentAppingOfTeacher.do", { teacherId: this.teacherId, page:e, rows:5, classCourseId: this.classcourseId,applyState:0}).subscribe(item => {
      this.slides = item['rows']
      if(this.slides.length==0){
        this.navCtrl.push(TeachingPage)
      }
      this.slides.forEach(e=>{
        e.apply_date.time = this.dataPipe.transform(e.apply_date.time,'yyyy-MM-dd');
        e.image_path = this.http.filePath+e.image_path
      })
      this.total = item['total'];
      console.log(123456)
      console.log(item)
    })
  }

  /**
 * 下拉刷新
 * @param refresher
 */
  async doRefresh(refresher) {
    let loading = this.loadingCtrl.create({
      content: '刷新中...',
      showBackdrop: false
    });
    loading.present();
    this.LoadList(1);

    setTimeout(() => {
      refresher.complete();
      loading.dismiss();
    }, 2000);
  }

  doSearch(e){
    this.LoadList(e);
  }
  agreestudent(id,name,sta){
    this.presentConfirmagre(id,name,sta);

  }
  repulsestudent(id,name,sta){
    this.presentConfirmreject(id,name,sta);

  }

  //点击同意提示
  presentConfirmagre(id,name,sta) {

    let alert = this.alertCtrl.create({
      title: '提示！',
      message: `确认同意新增 ${name} 吗？`,
      buttons: [
        {
          text: '取消',
          role: 'cancel',
          handler: () => {

          }
        },
        {
          text: '确认',
          handler: () => {
            if(sta==1){
              this.ID=id;
              this.http.post("/teacherStudentController/updateStudentTeacherState.do", { teacherId: this.teacherId, classCourseId: this.classcourseId,applyState:1,studentId:id}).subscribe(item => {
                this.LoadList(1);
              })
            }
          }
        }
      ]
    });
    alert.present();
  }
//拒接
  presentConfirmreject(id,name,sta) {

    let alert = this.alertCtrl.create({
      title: '提示！',
      message: `确认拒绝新增 ${name} 吗？`,
      buttons: [
        {
          text: '取消',
          role: 'cancel',
          handler: () => {

          }
        },
        {
          text: '确认',
          handler: () => {
           if(sta==2){
              this.ID=id;
              this.http.post("/teacherStudentController/updateStudentTeacherState.do", { teacherId: this.teacherId, classCourseId: this.classcourseId,applyState:2,studentId:id}).subscribe(item => {
                this.LoadList(1);
              })
            }
          }
        }
      ]
    });
    alert.present();
  }

}
